﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class StatisticData_CPKStatistic_CPKbyPID_Month : System.Web.UI.Page
{
    private Customer cust;
    private ProductType pt;
    private Test_QT t_qt;
    private TestItem_CPK tq;
    private string var_productTypeID
        {
        get { return ViewState["var_productTypeID"].ToString(); }
        set { ViewState["var_productTypeID"] = value; }
    }
    private string timeYear
    {
        get { return ViewState["timeYear"].ToString(); }
        set { ViewState["timeYear"] = value; }
    }

    public StatisticData_CPKStatistic_CPKbyPID_Month()
    {
        cust = new Customer();
        pt = new ProductType();
        t_qt = new Test_QT();
        tq = new TestItem_CPK();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Page.Unload += new EventHandler(Page_Unload);
            BindTimeMonth(); 
            BindCustoms();
            BindProductTypes();
        }
    }

    protected void BindCustoms()
    {
        this.DropDownList1.DataSource = cust.LoadEntities();
        this.DropDownList1.DataTextField = "CustomerName";
        this.DropDownList1.DataValueField = "CustomerID";
        this.DropDownList1.DataBind();
    }

    protected void BindProductTypes()
    {
        this.DropDownList3.DataSource = pt.GetProductTypesbyCustomerID(this.DropDownList1.SelectedValue);
        this.DropDownList3.DataTextField = "ProductTypeName";
        this.DropDownList3.DataValueField = "ProductTypeID";
        this.DropDownList3.DataBind();
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindProductTypes();
    }

    void Page_Unload(object sender, EventArgs e)
    {
        this.CrystalReportSource1.Dispose();
    }

    protected void BindTimeMonth()
    {
        this.DropDownList2.DataSource = t_qt.GetTimeM();
        this.DropDownList2.DataTextField = "DayTime";
        this.DropDownList2.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        var_productTypeID = this.DropDownList3.SelectedValue;
        timeYear = this.DropDownList2.SelectedItem.Text.Trim();
        BindCPKCR(var_productTypeID, timeYear);
        //BindCPK();
    }

    private void BindCPKCR(string var_productTypeID, string timeYear)
    {
        this.CrystalReportSource1.ReportDocument.Load(Server.MapPath("CPKbyPID_Month.rpt"));
        IList<CPKInfo> list = GetData(var_productTypeID, timeYear);
        this.CrystalReportSource1.ReportDocument.SetDataSource(list);
        this.CrystalReportSource1.DataBind();
        this.CrystalReportViewer1.ReportSource = CrystalReportSource1;
        this.CrystalReportViewer1.DataBind();
    }

    private IList<CPKInfo> GetData(string var_productTypeID, string timeYear)
    {
        IList<CPKInfo> CPKs = new List<CPKInfo>();
        CPKs = tq.GetCPKbyPID_Month(timeYear, var_productTypeID);
        return CPKs;
    }

    private void BindCPK()
    {
        var_productTypeID = this.DropDownList3.SelectedValue;
        timeYear = this.DropDownList2.SelectedItem.Text.Trim();

        this.GridView1.DataSource = tq.GetCPKbyPID_Month(timeYear,var_productTypeID);
        this.GridView1.DataBind();
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            if (GridView1.Rows[i].Cells[2].Text == "-1.000")
                GridView1.Rows[i].Cells[2].Text = "-";
            if (GridView1.Rows[i].Cells[3].Text == "-1.000")
                GridView1.Rows[i].Cells[3].Text = "-";
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindCPK();
    }
}
